﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Etong.Web;
public partial class Setup_GlobalSettings : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Themes"));
            var list = from d in dir.GetDirectories()
                       select new { name = d.Name };
            
            this.DropDownList1.DataTextField = "name";
            this.DropDownList1.DataValueField = "name";
            this.DropDownList1.DataSource = list;
            this.DropDownList1.DataBind();
            
            this.dbtype.DataSource = Enum.GetNames(typeof(DBType));
            this.dbtype.DataBind();

            ConfigGlobalSettings globalsettings = ConfigGlobal.GetGlobal();

            this.dbtype.SelectedValue = globalsettings.DbType.ToString();
            this.connstrname.Text = globalsettings.ConnectionStringName;
            this.admintablename.Text = globalsettings.AdminTableName;
            this.admintablepk.Text = globalsettings.AdminPKColumnField;
            this.unamecolumnname.Text = globalsettings.UserNameColumnField;
            this.passwdcolumnname.Text = globalsettings.PasswordColumnField;
            this.allowbrow.Checked = globalsettings.AllowBrowseControl;
            this.allowrole.Checked = globalsettings.AllowRole;
            this.roletablename.Text = globalsettings.RoleTableName;
            this.rolecolumnname.Text = globalsettings.RoleColumnField;
            this.rolePk.Text = globalsettings.RoleTablePkFieldName;
            this.roleName.Text = globalsettings.RoleTableNameFieldName;
            this.admindir.Text = globalsettings.AdminDir;
            if (!string.IsNullOrEmpty(globalsettings.Theme.Trim()))
            {
                this.DropDownList1.SelectedValue = globalsettings.Theme;
            }
        }        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ConfigGlobalSettings globalsettings = ConfigGlobal.GetGlobal();

        globalsettings.ConnectionStringName = this.connstrname.Text;
        globalsettings.DbType = (DBType)Enum.Parse(typeof(DBType), this.dbtype.SelectedValue);
        globalsettings.AdminTableName = this.admintablename.Text;
        globalsettings.AdminPKColumnField = this.admintablepk.Text;
        globalsettings.UserNameColumnField = this.unamecolumnname.Text;
        globalsettings.PasswordColumnField = this.passwdcolumnname.Text;
        globalsettings.AllowBrowseControl = this.allowbrow.Checked;
        globalsettings.AllowRole = this.allowrole.Checked;
        globalsettings.RoleTableName = this.roletablename.Text;
        globalsettings.RoleColumnField = this.rolecolumnname.Text;
        globalsettings.RoleTablePkFieldName = this.rolePk.Text;
        globalsettings.RoleTableNameFieldName = this.roleName.Text;
        globalsettings.Theme = this.DropDownList1.SelectedValue.Trim();
        globalsettings.AdminDir = this.admindir.Text;

        globalsettings.Save();
    }
}
